//
//  ViewController.swift
//  网络请求
//
//  Created by emma on 2024/4/19.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let iv = UIImageView(frame: CGRect(x: 20, y: 90, width: 300, height: 300))
        iv.contentMode = .scaleAspectFit
        view.addSubview(iv)
        let urlString = "https://img0.baidu.com/it/u=2646368451,349415709&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
        HttpRequest.download(urlString) { data in
            if let data = data {
                iv.image = UIImage(data: data)
            }
        } failure: { error in
            MyLog("下载失败")
        }

    }


}

//MARK: - 自定义打印
func MyLog<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
    #if DEBUG
        let fileName = (file as NSString).lastPathComponent
        print("[\(fileName)--\(lineNum)行--]:\(message)")
        //print("文件:\(fileName)\n方法:\(funcName)\n行号:\(lineNum)\n打印信息\(message)");
    #endif
}

extension String {
    //MARK: - 获取宽度、高度
    func getWidth(_ font: UIFont, _ height: CGFloat) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width:900, height: height), options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil).size
        return size.width
    }
    func getHeight(_ font: UIFont, _ width: CGFloat) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width:width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil).size
        return size.height
    }
}

extension Dictionary {
    ///默认值false
    func bool(_ key: String) -> Bool {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return (data[key]! as? Bool) ?? false
            }
        }
        return false
    }
    func bool(_ key: String,  _ defaultValue: Bool) -> Bool {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return (data[key]! as? Bool) ?? defaultValue
            }
        }
        return defaultValue
    }
    func int(_ key: String, _ defaultValue: Int) -> Int {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return (data[key]! as? Int) ?? defaultValue
            }
        }
        return defaultValue
    }
    ///默认值是0
    func int(_ key: String) -> Int {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return Int("\(data[key]!)") ?? 0
            }
        }
        return 0
    }
    ///默认值是""
    func string(_ key: String) -> String {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return "\(data[key]!)"
            }
        }
        return ""
    }
    func string(_ key: String, _ defaultValue: String) -> String {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return "\(data[key]!)"
            }
        }
        return defaultValue
    }
    func dictionary(_ key: String) -> [String : Any] {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            let content = (data[key] as? Dictionary<String, Any>)
            if content != nil {
                return content!
            }else {
                return [String : Any]()
            }
        }
        return [String : Any]()
    }
    func array(_ key: String) -> [[String : Any]] {
        let array = self as? [String : Any]
        //MyLog(array)
        if let data = array {
            let content = (data[key] as? [[String : Any]])
            if content != nil { return content! }
            else { return [[String : Any]]() }
        }
        return [[String : Any]]()
    }
    func dictionaryArray(_ key: String) -> [[String : Any]] {
        let dict = self as? [String : Any]
        if let data = dict {
            let content = (data[key] as? [[String : Any]])
            if content != nil { return content! }
            else { return [[String : Any]]() }
        }
        return [[String : Any]]()
    }
    func stringArray(_ key: String) -> [String] {
        let array = self as? [String : Any]
        if let data = array {
            let content = (data[key] as? [String])
            if content != nil { return content! }
            else { return [String]() }
        }
        return [String]()
    }
}
